﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.Currency.TextUtils;
using RabbitDownload.Helper.Currency;

using System;
using System.Collections.Generic;

namespace RabbitDownload.WebSite.Yande.All
{
    internal class YandeAllScanner
    {
        private static bool ReturnFlag = false;

        public static void Start(string StartPage, string EndPage, string API)
        {
            ReturnFlag = false;
            int StartCount = TextHelper.StringToInt(StartPage);
            int EndCount = TextHelper.StringToInt(EndPage);

            if (EndCount < StartCount || StartCount == 0)
            {
                Console.WriteLine("起始页小于结束页");
                return;
            }
            List<string> ArticleList = GetArticleList(StartCount, EndCount, API);
            WebCurrency.SaveHelper.AutoSave(ArticleList, "Yande", "ALL");
        }

        private static List<string> GetArticleList(int startCount, int endCount, string API)
        {
            Console.WriteLine(API);
            List<string> AllImg = new List<string>();

            for (int page = startCount; page <= endCount; page++)
            {
                Console.WriteLine("第{0}页正在扫描", page);

                String TempAPI = "";

                TempAPI = string.Format(API, page);
                Console.WriteLine("扫描API" + TempAPI);
                string Json = YandeHelper.GetJson(TempAPI);
                if (Json != "")
                    AllImg.AddRange(AnalysisJson(Json));
                if (ReturnFlag)
                {
                    ReturnFlag = false;
                    break;
                }
            }
            Console.WriteLine("扫描结束,总数" + AllImg.Count);
            return AllImg;
        }

        /// <summary>
        /// f分析json 取得结果
        ///
        /// </summary>
        /// <param name="json"></param>
        private static List<string> AnalysisJson(string json)
        {
            List<YandeJsonClass.PostItem> list = YandeHelper.InitPostJson(json);
            List<string> ImgList = new List<string>();

            if (list == null)
            {
                ReturnFlag = true;
                return ImgList;
            }
            foreach (YandeJsonClass.PostItem Single in list)
            {
                if (YandeStaticValue.ExcludeFlag && YandeHelper.GetJudgeTags(Single.tags))
                {
                    Console.WriteLine("检测到黑名单关键词，跳出！");
                }
                if (!YandeHelper.GetModeFlag(Single.rating, Single.score))
                {
                    continue;
                }
                if (HandlerLog(Single.id))
                {
                    Console.WriteLine("已经下载过的");
                    continue;
                }

                ImgList.Add(Single.file_url);
            }
            return ImgList;
        }

        /// <summary>
        /// 判断日志是否处理过
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private static bool HandlerLog(string id)
        {
            if (Yande.YandeStaticValue.UseLogFlag1)
            {
                if (Yande.YandeStaticValue.YandeOldList1.Contains(id.Trim())) { return true; }
                else
                {
                    FileHelper.AppendText(YandeStaticValue.YandeOldListPath1, id);
                    YandeStaticValue.YandeOldList1.Add(id);//更新日志
                }
            }

            return false;
        }
    }
}